<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            display: block;
            border: none;
            border-radius: 5px;
            background-color: lavender;
            padding-left: 10px;
            width: 400px;
            height: 30px;
            box-sizing: border-box;
            margin-top: 10px;
            margin-left: 400px;
        }

        button {
            display: block;
            border: none;
            border-radius: 5px;
            width: 400px;
            height: 30px;
            background-color: lightblue;
            margin-top: 10px;
            margin-left: 400px;
        }
    </style>
</head>

<body>
    <input type="text" placeholder="手机号/用户名/邮箱">
    <input type="password" placeholder="密码">
    <button>登录</button>
</body>

</html>
<!-- <script src="../promise-ajax.js"></script> -->
<script>
    function Search(obj) {
        var str = ``
        for (const key in obj) {
            str += `${key}=${obj[key]}&`
        }
        return str.slice(0, str.length - 1)
    }
    function ajax(method, url, params) {
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest()
            xhr.open(method, method == 'get' ? url + '?' + Search(params) : url)
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.send(method == 'get' ? null : Search(params))
        })
    }
    var inEle = document.querySelectorAll('input')
    var btn = document.querySelector('button')
    btn.onclick = function () {
        if (inEle[0].value && inEle[1].value) {
            if (/^\w+@[0-9a-z]+\.(com|cn|org)$/ || /^\d{4}$/ || /^1[3-9]\d{9}$/.test(inEle[0].value) && /^\d{4}$/.test(inEle[1].value)) {
                ajax('get','https://yantianfeng.com/api/login',{
                    username:inEle[0].value,
                    password:inEle[1].value
                }).then((data)=>{
                    console.log(data);
                    if (data.status ==200) {
                        console.log('登录成功');
                    }else{
                        console.log('登录失败');
                    }
                })
            }else{
                alert('请输入正确的手机号/密码/邮箱/用户名')
            }
        } else {
            alert('不能为空')
        }
    }
</script>